package com.gitee.feizns.explore.data_structure.array.easy;

/**
 * @author feizns
 * @since 2020/1/13
 */
public class MatrixReshape {

    public static void main(String[] args) {
        final int[][] ints = matrixReshape(new int[][]{{1, 2}, {3, 4}}, 1, 4);
        print(ints);
    }

    private static void print(int[][] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] matrixReshape(int[][] num, int r, int c) {
        if ( num.length * num[0].length == r * c ) {
            int[][] ret = new int[r][c];
            int x = 0;
            int y = 0;
            for (int i = 0; i < r; i++) {
                for (int j = 0; j < c; j++) {
                    ret[i][j] = num[x][y++];
                    if ( y == num[0].length ) {
                        x++;
                        y = 0;
                    }
                }
            }
            return ret;
        }
        return num;
    }

}
